<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{common/base :: layout(~{::title}, ~{::content}, ~{::style}, ~{::script})}">
<head>
    <title>订单支付 - 优选商城</title>
    <style>
        .order-info {
            background-color: #f8f9fa;
            padding: 20px;
            border-radius: 5px;
        }
        .payment-method {
            padding: 15px;
            border-radius: 5px;
            border: 1px solid #dee2e6;
            margin-bottom: 15px;
            cursor: pointer;
            transition: all 0.3s;
        }
        .payment-method:hover {
            border-color: #0d6efd;
        }
        .payment-method.selected {
            border-color: #0d6efd;
            background-color: #f0f7ff;
        }
        .payment-method .payment-logo {
            width: 60px;
            height: 40px;
            object-fit: contain;
        }
        .payment-countdown {
            background-color: #fff3cd;
            color: #856404;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .order-detail {
            margin-top: 30px;
        }
        .qr-code {
            width: 200px;
            height: 200px;
            margin: 30px auto;
        }
    </style>
</head>
<body>
    <div th:fragment="content">
        <div class="container my-4">
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <h2 class="mb-4">订单支付</h2>
                    
                    <div th:if="${order != null}">
                        <!-- 订单倒计时 -->
                        <div class="payment-countdown">
                            <div class="d-flex justify-content-between align-items-center">
                                <div>
                                    <i class="bi bi-clock me-2"></i> 请在 <span id="countdown">15:00</span> 内完成支付
                                </div>
                                <div>
                                    订单号: <span th:text="${order.orderNo}">20250607123456</span>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 订单信息 -->
                        <div class="card mb-4">
                            <div class="card-header bg-white">
                                <h5 class="mb-0">订单信息</h5>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-4">
                                        <p class="mb-1">订单金额</p>
                                        <h4 class="text-danger">¥<span th:text="${#numbers.formatDecimal(order.payment, 1, 2)}">0.00</span></h4>
                                    </div>
                                    <div class="col-md-8">
                                        <p class="mb-1">收货信息</p>
                                        <p class="mb-0">
                                            <span th:text="${order.shippingAddress?.receiver}">收件人</span>
                                            <span th:text="${order.shippingAddress?.phone}">电话</span>
                                        </p>
                                        <p class="text-muted mb-0" 
                                           th:text="${order.shippingAddress?.province} + ' ' + ${order.shippingAddress?.city} + ' ' + ${order.shippingAddress?.district} + ' ' + ${order.shippingAddress?.detail}">
                                            地址详情
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 支付方式 -->
                        <div class="card mb-4">
                            <div class="card-header bg-white">
                                <h5 class="mb-0">支付方式</h5>
                            </div>
                            <div class="card-body">
                                <div class="payment-method selected" onclick="selectPayment(this, 'alipay')">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div class="d-flex align-items-center">
                                            <img src="https://img.alicdn.com/imgextra/i2/O1CN01oolfoN1JVKx5LDJZn_!!6000000001034-55-tps-95-28.svg" 
                                                 class="payment-logo me-3" alt="支付宝">
                                            <div>
                                                <h6 class="mb-0">支付宝</h6>
                                                <small class="text-muted">推荐使用支付宝支付</small>
                                            </div>
                                        </div>
                                        <div>
                                            <i class="bi bi-check-circle-fill text-primary"></i>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="payment-method" onclick="selectPayment(this, 'wechat')">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div class="d-flex align-items-center">
                                            <img src="https://res.wx.qq.com/wxp/zh_CN/htmledition/images/weixin-logo.svg" 
                                                 class="payment-logo me-3" alt="微信支付">
                                            <div>
                                                <h6 class="mb-0">微信支付</h6>
                                                <small class="text-muted">使用微信扫码支付</small>
                                            </div>
                                        </div>
                                        <div>
                                            <i class="bi bi-check-circle-fill text-primary d-none"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 支付按钮 -->
                        <div class="text-center">
                            <button id="payButton" class="btn btn-primary btn-lg px-5" 
                                    th:data-order-no="${order != null ? order.orderNo : ''}"
                                    onclick="payOrder()">
                                立即支付
                            </button>
                            <p class="mt-3">
                                <a th:href="@{'/order/detail/' + ${order.orderNo}}" class="text-muted">稍后支付</a>
                            </p>
                        </div>
                    </div>
                    
                    <!-- 订单不存在提示 -->
                    <div th:if="${order == null}" class="alert alert-warning">
                        <h4 class="alert-heading">订单不存在或已过期</h4>
                        <p>可能的原因：</p>
                        <ul>
                            <li>订单已被取消</li>
                            <li>订单创建失败</li>
                            <li>会话已过期，请重新登录</li>
                        </ul>
                        <p th:if="${error}" class="mt-2">
                            <strong>错误信息:</strong> <span th:text="${error}">未知错误</span>
                        </p>
                        <p th:if="${errorDetail}" class="text-muted small mt-2">
                            <span th:text="${errorDetail}">详细错误信息</span>
                        </p>
                        <hr>
                        <p class="mb-0">
                            <a href="/cart" class="btn btn-primary me-2">返回购物车</a>
                            <a href="/" class="btn btn-outline-secondary">返回首页</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script th:fragment="script">
        let selectedPayment = 'alipay';
        
        $(document).ready(function() {
            // 初始化倒计时
                startCountdown();
        });
        
        // 选择支付方式
        function selectPayment(element, paymentType) {
            // 更新选中状态
            $('.payment-method').removeClass('selected');
            $('.payment-method .bi-check-circle-fill').addClass('d-none');
            
            $(element).addClass('selected');
            $(element).find('.bi-check-circle-fill').removeClass('d-none');
            
            // 保存选中的支付方式
            selectedPayment = paymentType;
        }
        
        // 开始倒计时
        function startCountdown() {
            let minutes = 15;
            let seconds = 0;
            
            const countdownInterval = setInterval(function() {
                if (seconds === 0) {
                    if (minutes === 0) {
                        clearInterval(countdownInterval);
                        showToast('支付超时，订单已取消', 'warning');
                        setTimeout(function() {
                            window.location.href = '/order/list';
                        }, 2000);
                        return;
                    }
                    minutes--;
                    seconds = 59;
                } else {
                    seconds--;
                }
                
                // 更新显示
                const minutesStr = minutes.toString().padStart(2, '0');
                const secondsStr = seconds.toString().padStart(2, '0');
                $('#countdown').text(`${minutesStr}:${secondsStr}`);
            }, 1000);
        }
        
        // 支付订单
        function payOrder() {
            // 从按钮属性获取订单号
            const orderNo = $('#payButton').data('orderNo');
            console.log("支付订单号:", orderNo);
            
            if (!orderNo) {
                showToast('订单号为空，无法支付', 'danger');
                return;
            }
            
            $('#payButton').prop('disabled', true);
            
            // 直接发送支付请求，无需显示二维码
            $.ajax({
                url: '/order/pay/' + orderNo,
                type: 'POST',
                success: function(res) {
                    if (res.code === 0) {
                        showToast('支付成功', 'success');
                        setTimeout(function() {
                            window.location.href = '/order/detail/' + orderNo;
                        }, 1500);
                    } else {
                        showToast(res.msg || '支付失败', 'danger');
                        $('#payButton').prop('disabled', false);
                    }
                },
                error: function() {
                    showToast('请求失败，请重试', 'danger');
                    $('#payButton').prop('disabled', false);
                }
            });
        }
        
        // 检查支付状态 - 此方法不再需要，但保留以免影响其他功能
        function checkPaymentStatus() {
            // 这个方法已被简化的payOrder()取代
            payOrder();
        }
        
        // 显示提示
        function showToast(message, type) {
            const toastContainer = document.querySelector('.toast-container');
            if (!toastContainer) return;
            
            const toast = document.createElement('div');
            toast.className = `toast align-items-center text-white bg-${type} border-0`;
            toast.setAttribute('role', 'alert');
            toast.setAttribute('aria-live', 'assertive');
            toast.setAttribute('aria-atomic', 'true');
            
            toast.innerHTML = `
                <div class="d-flex">
                    <div class="toast-body">
                        <i class="bi bi-${type === 'success' ? 'check-circle' : type === 'warning' ? 'exclamation-triangle' : type === 'info' ? 'info-circle' : 'exclamation-circle'} me-2"></i>
                        ${message}
                    </div>
                    <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
                </div>
            `;
            
            toastContainer.appendChild(toast);
            const bsToast = new bootstrap.Toast(toast, {
                autohide: true,
                delay: 3000
            });
            
            bsToast.show();
            
            // 自动移除toast元素
            toast.addEventListener('hidden.bs.toast', function () {
                toast.remove();
            });
        }
    </script>
</body>
</html>